<?php

// this file will setup Small Axe 1.0 into a MySQL database. 
// if you want, you can create your own database for Small Axe
// using the included smallaxe.sql file

if($_POST) { 
$db_server 		= $_POST['db_server']; 
$db_user		= stripslashes($_POST['db_user']); 
$db_password	= stripslashes($_POST['db_password']);
$db_database	= $_POST['db_database']; 
$mysql_error 	= "There was an error querying the MySQL database!"; 
$tbl_prefix		= strip_tags(stripslashes($_POST['db_prefix']));

	// connect to database, first and foremost
	$conn = mysql_connect($db_server, $db_user, $db_password);
	if (!$conn) { 
		die("Could not connect: ".$mysql_error);
	}
	mysql_select_db($db_database);
}

function mysql_table_exists2($table){
     $exists = @mysql_query("SELECT 1 FROM $table LIMIT 0");
     if ($exists) { return true; } else { return false; }
}
$smallaxe = @mysql_table_exists2($tbl_prefix."content"); 


if($_POST['action']=="build") { 
	if($smallaxe) { 
		die("It appears Small Axe has already been installed!"); 
	}
	$sql = "CREATE TABLE `".$tbl_prefix."comments` (
  `commentid` int(5) NOT NULL default '0',
  `id` int(5) NOT NULL default '0',
  `name` varchar(55) default 'Anonymous',
  `contact` varchar(55) default NULL,
  `title` varchar(255) NOT NULL default '',
  `text` text NOT NULL,
  `ip` varchar (16) NOT NULL,
  `score` tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (`commentid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;"; 
	$result = mysql_query($sql); 
	$sql = "CREATE TABLE `".$tbl_prefix."content` (
  `id` int(11) NOT NULL default '0',
  `pubdate` datetime default NULL,
  `title` varchar(255) NOT NULL default '',
  `slug` varchar(55) default NULL, 
  `teaser` text NOT NULL,
  `post` text,
  `public` int(1) NOT NULL default '1',
  `uid` int(4) NOT NULL default '1',
  `topic` int(4) NOT NULL default '1',
  `views` int(5) NOT NULL default '1',   
  `locked` int(1) NOT NULL default '0', 
  PRIMARY KEY  (`id`),
  UNIQUE KEY `slug` (`slug`),
  FULLTEXT KEY `post` (`post`),
  FULLTEXT KEY `title` (`title`),
  FULLTEXT KEY `teaser` (`teaser`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT='Small Axe data'";
	$result = mysql_query($sql); 
	$sql = "CREATE TABLE `".$tbl_prefix."tags` (
  `tagid` int(11) NOT NULL auto_increment,
  `id` int(11) NOT NULL default '0',
  `tag` varchar(255) NOT NULL default '',
  `tagname` varchar(255) default NULL,
  PRIMARY KEY  (`tagid`),
  KEY `tag` (`tag`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='Tags';"; 
	$result = mysql_query($sql);	
	$sql = "CREATE TABLE `".$tbl_prefix."uagents` (
  `agent_id` int(11) NOT NULL auto_increment,
  `page` varchar(100) NOT NULL default '',
  `agent` varchar(255) NOT NULL default '',
  `ip` varchar(16) NOT NULL default '',
  `tstamp` varchar(11) NOT NULL default '',
  `referer` varchar(255) NOT NULL default '127.0.0.1',
  PRIMARY KEY  (`agent_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;"; 
	$result = mysql_query($sql);	
	$sql = "CREATE TABLE `".$tbl_prefix."topics` (
  `topicid` int(4) NOT NULL auto_increment,
  `name` varchar(55) NOT NULL default '',
  `img` varchar(55) NOT NULL default 'default.png',
  `active` tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (`topicid`),
  UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;"; 

	$result = mysql_query($sql); 
	$sql = "CREATE TABLE `".$tbl_prefix."users` (
  `uid` int(5) NOT NULL auto_increment,
  `username` varchar(55) NOT NULL default '',
  `password` varchar(55) NOT NULL default '',
  `realname` varchar(55) NOT NULL default '',
  `email` varchar(100) default NULL,
  `perms` tinyint(1) NOT NULL default '1',
  `active` tinyint(1) NOT NULL default '1',
  `hash` varchar(29) NOT NULL default '',
  PRIMARY KEY  (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2"; 
	$result = mysql_query($sql); 	
	if(!$result) { echo "Error 1: There may have been an error creating the database.<br />"; }
	unset($sql); @mysql_free_result($result); 
	
	$firstID = time();
	$firstDate = date("Y-m-d G:i:s",$firstID); 
	//insert default records
	$sql = "INSERT INTO `".$tbl_prefix."topics` VALUES (1, 'Default', 'default.png', 1);"; 
	$result = mysql_query($sql); 
	$sql = "INSERT INTO `".$tbl_prefix."users` VALUES (1, 'admin', 'd033e22ae348aeb5660fc2140aec35850c4da997', 'Admin', 'you@yoursite.com', 1, 1, 'kjlbHVsfasdfouiebrfnasdf')"; 
	$result = mysql_query($sql); 
	$sql = "INSERT INTO ".$tbl_prefix."tags VALUES (1,'".$firstID."','Weblog','weblog');";
	$result = mysql_query($sql);
	$sql = "INSERT INTO ".$tbl_prefix."tags VALUES (2,'".$firstID."','Online Apps','online_apps');";	
	$result = mysql_query($sql);
	$sql = "INSERT INTO `".$tbl_prefix."content` VALUES (".$firstID.", '".$firstDate."', 'Small Axe on ".$_SERVER['HTTP_HOST']
	."','Small-Axe-on-".trim($_SERVER['HTTP_HOST'])."', 'Welcome to [[http://smallaxesolutions.com|Small Axe]] 0.4, codename \"Hattori.\"  Read on for details.', 'Here is some information about your install of Small Axe 0.4: \r\n* It runs on MySQL only.  Our next release will include support for plain text files (flatfiles).  A future version will run on SQLite, and we are looking to eventually support PostgreSQL and/or Microsoft SQL Server.  The goal is to support all five.  \r\n* Unlike current versions of Flip, it supports a \"teaser\" and a body.  The teaser is displayed on the front page, the body is linked, but not required. \r\n* The URLs are pretty: they look like this: \r\nhttp://server/public/path/Title-of-Entry.\r\n* There will be an upgrade path, however, since it is a complete rewrite, it may be slightly rocky.  \r\n* The system will move to sha1() encryption, instead of MD5.\r\n* This version supports image uploads, if you server supports it.\r\n* The templates are pure HTML. Anyone with HTML experience can edit the template. \r\n* The system will be smart enough to do some cool tricks, like automagically transforming links like this: [[url&pipe;description]] Here''s a demo of it: [[http://smallaxesolutions.com|Small Axe Solutions]]\r\n* Enhanced CSS/Javascript make for \"dynamic\" display of comments.  This is a really cool effect. \r\n* \"Publication Date\" of posts can be customized, so you can publish in the future and have it automagically appear later. \n\r* Entries can be \"tagged\" multiple times.  The system can make use of topics OR tags as your primary categorization.\r\n* Built in support for RSS 2.0 and Atom 1.0 and 0.3 is present.  Support for static RSS and Atom pages, such as cron-generated pages, is present. \r\n* \"Query counter\" to help gauge performance. \r\n* Built in \"debug\" mode, which throws useful errors. \r\n* Last, but most certainly not least, valid XHTML/CSS out of the box. \r\n\r\nThere are several features that will be present in 1.0 that are not present in this release.  Missing features and known limitations include: \r\n* User and and topic management is incomplete.  The default login name is \"admin\" with a password of \"admin\" althugh it can be changed.  Your \"display name\" can be changed. \r\n*There is currently no comment moderation.  All comments are scored \"1,\" or \"visible.\"', 1, 1, 1, 1 ,0)"; 
	$result = mysql_query($sql); 
	if(!$result) { echo "Error 2: There may have been an error inserting records into the database.<br />"; }
	unset($sql); @mysql_free_result($result); 
	
	$sql = "SELECT * FROM ".$tbl_prefix."content WHERE id = '".$firstID."'";
	$result = mysql_query($sql); 
	
	if($result) { 
		echo "Table setup completed.  Please delete the database install files (the /install directory, which includes setup.php and smallaxe.sql)!<br /><br />You may now view your Small Axe blog.";
	} else { 
		echo "Error 3: Please check if there is a \"content\" table in the ".$db_database." database.<br />"; 
	}
} elseif(!$smallaxe) { 
	echo "<html>\n\t<head>\n\t\t<title>Install Small Axe</title>\n\t</head>\n\t<body>\n\t\tClick the below button to install <b>Small Axe</b> into a MySQL database\n\t\t<br /><br />\n\t\t<form name=\"dbsetup\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n\t\t<input type=\"hidden\" name=\"action\" value=\"build\" />\n"; ?>
	Please enter the name of your database server:<br />
	<input type="text" size="50" maxlength="55" name="db_server" value="localhost" /><br /><br />
	Please enter the name of your database user:<br />
	<input type="text" size="50" maxlength="55" name="db_user" /><br /><br />
	Please enter the password of your database user:<br />
	<input type="password" size="50" maxlength="55" name="db_password" /><br /><br />
	Please enter the name of your "Small Axe" database:<br />
	<input type="text" size="50" maxlength="55" name="db_database" /><br /><br />	
	If you wish to prepend your tables with a prefix, enter it here:<br />
	<input type="text" size="50" maxlength="55" value="smallaxe_" name="db_prefix" /><br /><br />
	<?php echo "\t\t<input type=\"submit\" name=\"build\" value=\"Build Small Axe Now\" />\n\t\t</form>\n\t</body>\n</html>\n";
} else { 
	echo "It appears Small Axe has already been installed.";
}
?> 